<template>
  <el-container>
    <el-header>
      <div class="report_header" ref="headerHeight">
        <div class="report_header_left"></div>
        <!-- <div style="width: 300px;display: flex;align-items: center;margin-left: 10px;">
          <svg-icon class="primary" :icon-class="$route.meta.icon"/>
          <span style="margin-left: 5px;" class="primary">{{$route.meta.title}}</span>
        </div> -->
        <div style="display: flex;flex-direction: row;justify-content:space-between;width: 100%;">
          <div style="display: flex;flex-direction: row;flex-wrap: wrap;overflow: hidden;height:41px">

            <div class="report_header_query">
              <el-input prefix-icon="el-icon-search" placeholder="名称"  v-model="query.tplName"></el-input>
            </div>
            <div class="report_header_query">
              <el-input prefix-icon="el-icon-search" placeholder="键值"  v-model="query.tplKey"></el-input>
            </div>
          </div>
          <div>
          <el-button style="margin-left: 10px;margin-top: 5px;" size="small" type="primary" @click="querySysUser">查询</el-button>
          <el-button style="margin-left: 10px;margin-top: 5px;" size="small" type="primary" @click="addClick"
                     v-permission="['sysTemplate.add']">新增</el-button>
          </div>
        </div>
      </div>
<!--      <div>-->
<!--        <svg-icon class="primary" :icon-class="$route.meta.icon"/>-->
<!--        <span class="primary">{{$route.meta.title}}</span>-->
<!--      </div>-->
<!--      <div class="header-button-container">-->
<!--        <el-button :icon="queryType? 'el-icon-search':'el-icon-close'" type="primary"-->
<!--                   @click="chooseClick">{{queryType == false?'隐藏':'查询'}}-->
<!--        </el-button>-->
<!--        <el-button v-permission="['sysTemplate.add']" icon="el-icon-plus" type="info" style="margin-left: 10px" @click="addClick">新增-->
<!--        </el-button>-->
<!--      </div>-->
    </el-header>
    <div class="pd10">
<!--      <div style="margin-bottom: 10px;display: flex;align-items: center" v-show="queryType == false">-->
<!--        <el-input prefix-icon="el-icon-search" placeholder="名称" style="width: 200px" v-model="query.tplName"></el-input>-->
<!--        <el-input prefix-icon="el-icon-search" placeholder="键值" style="width: 200px;margin-left: 10px" v-model="query.tplKey"></el-input>-->
<!--        <el-button style="margin-left: 10px" icon="el-icon-search" type="primary" @click="querySysUser">查询-->
<!--        </el-button>-->
<!--        <el-button style="margin-left: 10px" icon="el-icon-refresh-right" type="info" @click="resetSysUser">重置-->
<!--        </el-button>-->
<!--      </div>-->
      <el-table
        :data="sysTemplate"
        border
        stripe
        v-loadMore="[payLoads,'sysTemplate-list']"
        :height="curHeight"
        style="width: 100%">
        <el-table-column
          type="index"
          width="50"
          align="center"
          fixed
        >
        </el-table-column>
        <el-table-column
          label="模版名称"
          width="220"
          align="center"
          prop="tplName"
        >
        </el-table-column>
        <el-table-column
          label="模版内容"
          align="center"
          min-width="350"
          prop="tplContent"
        >
        </el-table-column>
        <el-table-column
          label="模版键值"
          width="220"
          align="center"
          prop="tplKey"
        >
        </el-table-column>
        <el-table-column
          label="模版类型"
          width="220"
          align="center"
          prop="tplType"
        >
          <template slot-scope="scope">
              <span>{{scope.row.tplType | type}}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="备注"
          align="center"
          min-width="350"
          prop="remark"
        >
        </el-table-column>
        <el-table-column label="操作" align="center" fixed="right" width="80">
          <template slot-scope="scope">
            <div>
              <el-tooltip v-permission="['sysTemplate.edit']" class="item" effect="dark" content="编辑" placement="top-start">
                <a  @click="editClick(scope.row)">
                  <i style="color: #409EFF" class="el-icon-edit"></i>
                </a>
              </el-tooltip>
              <el-tooltip v-permission="['sysTemplate.del']" class="item" effect="dark" content="删除" placement="top-start">
                <a style="margin-left: 10px"  @click="deleteSysUser(scope.row)">
                  <i style="color: #F56C6C" class="el-icon-delete"></i>
                </a>
              </el-tooltip>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-footer class="el-footerBox">
      <pagination v-show="total>0" :total="total" :page.sync="pageNum" :limit.sync="pageSize"
                  @pagination="loadTemplate"></pagination>
    </el-footer>
  </el-container>
</template>
<script>
  import loadMore from '@/utils/loadMore.js'
  import permission from '@/directive/permission/index.js' // 权限判断指令
  import {list,del} from '@/api/sysTemplate'
  import utils from '../../utils/utils.js'
  import Pagination from '@/components/Pagination';
  import {EventBus} from '@/api/eventBus'
  export default {
    name: 'sysTemplate-list',
    directives: { permission,loadMore},
    activated() {
      this.payLoads = new Date().getTime();
    },
    data() {
      return {
        payLoads:'',
        sysTemplate: [],
        pageNum: 1,
        pageSize: 10,
        total: 0,
        queryType: true,
        query: {
          tplName:'',
          tplKey:''
        },
        keyword:{
          tplName:'',
          tplKey:''
        },
        curHeight:null
      }
    },
    components: {Pagination},
    created() {
      var _this = this
      this.loadTemplate()
      EventBus.$on('sysTemplate-add', res => { // 监听新增完毕
        // 更新列表数组
        _this.sysTemplate.splice(0, 0,res)
      });
      EventBus.$on('sysTemplate-edit', res => { // 监听新增完毕
        // 更新列表数组
        for (var i=0;i<_this.sysTemplate.length;i++){
          if (_this.sysTemplate[i].id == res.id){
            _this.sysTemplate[i] = res
          }
        }
      })
    },
    filters: {
      type(data){
        if (data ==0){
          return '短信'
        } else if (data == 1) {
          return '邮箱'
        }else if (data ==2){
          return '通知'
        }
      }
    },
    mounted(){
      this.comeIn()
      this.beforeMount();
    },
    methods: {
      beforeMount() {
        var _this = this
        window.onresize = () => {
          return (() => {
            if (this.queryType == false) {
              var h = document.documentElement.clientHeight || document.body.clientHeight;
              _this.curHeight = h - 266//减去页面上固定高度height
            } else {
              var h = document.documentElement.clientHeight || document.body.clientHeight;
              _this.curHeight = h - 220//减去页面上固定高度height
            }
          })();
        };
      },
      comeIn(){
        console.log('222')
        var h = document.documentElement.clientHeight || document.body.clientHeight;
        this.curHeight = h - 220//减去页面上固定高度height
      },
      addClick() {
        this.$router.push({
          path: '/sysTemplate/add'
        })
      },
      editClick(row) {
        this.$router.push({
          path: '/sysTemplate/edit', query: {id: row.id}
        })
      },
      chooseClick() {
        this.queryType = !this.queryType
        if (this.queryType == false) {
          var h = document.documentElement.clientHeight || document.body.clientHeight;
          this.curHeight = h - 266//减去页面上固定高度height
        } else {
          var h = document.documentElement.clientHeight || document.body.clientHeight;
          this.curHeight = h - 220//减去页面上固定高度height
        }
      },
      deleteSysUser(data) {
        console.log(data)
        var _this = this
        this.$confirm('确认删除?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          del(data.id).then(response => {
            console.log(response)
            _this.sysTemplate.forEach(function (item,index) {
              if (item.id == data.id){
                _this.sysTemplate.splice(index,1)
              }
            });
            _this.$message({
              message: response.content,
              offset: 120,
              center: true
            });
          }).catch(error => {
            console.log('error!!')
          });
        }).catch(() => {
        });
      },
      querySysUser() {
        this.keyword = this.query;
        this.pageNum = 1
        this.loadTemplate()
      },
      resetSysUser() {
        this.query.tplName = '';
        this.query.tplKey = '';
        this.keyword = this.query;
        this.pageNum = 1;
        this.loadTemplate()
      },
      loadTemplate() {
        var _this = this
        let params = new URLSearchParams();
        params.append('pageNum', _this.pageNum);
        params.append('pageSize', _this.pageSize);
        params.append('tplName',_this.keyword.tplName);
        params.append('tplKey',_this.keyword.tplKey);
        list(params).then(response => {
          console.log(response)
          _this.sysTemplate = response.data.data
          _this.total = response.data.total
        }).catch(error => {
          console.log('error!!')
        });
      },
    }
  }
</script>

<style scoped>
  .el-footerBox {
    height: 50px !important;
    line-height: 50px !important;
    border-top: 1px solid #eeeeee;
  }

  /deep/ .el-dialog__body {
    padding: 10px 20px 15px 20px;
  }

  /deep/ .el-dialog__header {
    border: 1px solid #eeeeee !important;
    padding: 15px !important;
  }
  .app {
    background-color: rgba(248, 248, 248, 0.8);
  }

  /deep/.el-header {
    padding: 10px;
    /* height: 100% !important; */
    padding-bottom: 0;
  }

  .report_header {
    width: 100%;
    min-height: 40px;
    box-sizing: border-box;
    background-color: rgba(248, 248, 248, 0.8);
    display: flex;
    align-items: center;
    padding-bottom: 5px;
    position: relative;
    padding-left: 5px;
    padding-right: 5px;
  }
  .report_header_left {
    width: 5px;
    height: 100%;
    background-color: #409EFF;
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
  }

  .report_header_query {
    margin-left: 10px;
    width: 180px;
    margin-top: 5px;
  }
</style>
